#include "WiFly.h"
#include "Credentials.h"
#include <Wire.h>
#include <string.h>

#define GPM 0x68  	    // Adresse du peripherique du module gps

byte Address;               // Variables des registres du module gps
byte Data; 

char coor[30];               //string dans laquelle on stock les coordonnees gps
Server server(1919);

void setup() {
  WiFly.begin();

  WiFly.enableDHCP();
  if (!WiFly.join(ssid, passphrase,true)) {
    while (1) {
      // Hang on failure.
    }
  }

  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());

  server.begin();
  Wire.begin();
  delay(1000);
}

void loop() {
  Client client = server.available();
  if (client) {
    if (client.connected()) {
      while(client.available() < 1);
      char c1 = client.read();
      while(client.available() < 1);
      char c2 = client.read();

      if(c1=='#' && c2=='1'){
        refresh_coor();
        client.print("GPS ");
        client.println(coor);
      }
      if(c1=='#' && c2=='?'){
        client.println("OK GPS");
      }

      client.stop();

    }
    delay(100);

  }
}

void refresh_coor(){

   memset (coor,0,30);

  Address = 14;                                       // Point to Latitude 1 register
  coor[0] = GetSingle()+48;                                 // Read registers from GPM
  Address = 15;                                       // Point to Latitude 2 register
  coor[1] = GetSingle()+48;                                 // Read registers from GPM
  coor[2]=' ';
  Address = 16;                                       // Point to Latitude 3 register
  coor[3] = GetSingle()+48;                                // Read registers from GPM
  Address = 17;                                       // Point to Latitude 4 register
  coor[4] = GetSingle()+48;                                 // Read registers from GPM
  coor[5] = '.';
  Address = 18;                                       // Point to Latitude 5 register
  coor[6] = GetSingle()+48;                                 // Read registers from GPM
  Address = 19;                                       // Point to Latitude 6 register
  coor[7] = GetSingle()+48;                                 // Read registers from GPM
  Address = 20;                                       // Point to Latitude 7 register
  coor[8] = GetSingle()+48;                                 // Read registers from GPM
  Address = 21;                                       // Point to Latitude 8 register
  coor[9] = GetSingle()+48;                                 // Read registers from GPM
  Address = 22;                                       // Point to Latitude character register
  coor[10] = GetSingle();                                 // Read registers from GPM

  coor[11]=' ';

  Address = 23;                                       
  coor[12] = GetSingle()+48;                      
  Address = 24;                                       
  coor[13] = GetSingle()+48;                         
  Address = 25;                                       
  coor[14] = GetSingle()+48;
  coor[15] = ' ';
  Address = 26;                                       // Point to Longitude 4 register
  coor[16] = GetSingle()+48;
  Address = 27;                                       // Point to Longitude 5 register
  coor[17] = GetSingle()+48;
  coor[18] = '.';
  Address = 28;                                       // Point to Longitude 6 register
  coor[19] = GetSingle()+48;
  Address = 29;                                       // Point to Longitude 7 register
  coor[20] = GetSingle()+48;
  Address = 30;                                       // Point to Longitude 8 register
  coor[21] = GetSingle()+48;
  Address = 31;                                       // Point to Longitude 9 register
  coor[22] = GetSingle()+48;
  Address = 32;                                       // Point to Longitude character register
  coor[23] = GetSingle();
}

int GetSingle(){				      // Get single register value from GPM

  int Value = 0; 

  Wire.beginTransmission(GPM);
  Wire.send(Address);				      // Send register start address
  Wire.endTransmission();

  Wire.requestFrom(GPM, 1);			      // Request register
  while(Wire.available() < 1);			      // Wait for single byte
  Value = Wire.receive();                             // and store

    return(Value);	                          
}






